package com.hmdp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.service.IBlogService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    private IBlogService blogService;

    public BlogController(IBlogService blogService) {
        this.blogService = blogService;
    }


    /**
     * 保存笔记 并进行关注推送
     * @param blog 笔记
     * @return 笔记id
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return  blogService.saveBlog(blog);
    }



    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        // 修改点赞数量
        return blogService.likeBlog(id);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {

        return blogService.queryHotBlog(current);
    }


    /**
     * 显示笔记详情
     * @param id 笔记id
     * @return 无
     */
    @GetMapping("/{id}")
    public Result queryBlockById(@PathVariable("id") Long id){
        return blogService.queryBlockById(id);
    }


    /**
     * 查点赞top5用户
     * @param id 笔记id
     * @return wu
     */
    @GetMapping("/likes/{id}")
    public Result queryBlockLikes(@PathVariable("id") Long id){
        return blogService.queryBlockLikes(id);
    }


    /**
     * 根据用户id查询笔记
     * @param id 用户id
     * @param current 页码
     * @return 笔记
     */
    @GetMapping("/of/user")
    public Result queryBlogUserId(@RequestParam(value = "current",defaultValue = "1") Integer current,
                                  @RequestParam(value = "id") Long id){

        //根据用户查询
        Page<Blog> page = blogService.query().eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));

        //获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /**
     * 实现关注页码的分页实现
     * @param max 上一次查询的最小时间戳
     * @param offset 偏移量
     * @return 无
     */
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max,
                                    @RequestParam(value = "offset",defaultValue = "0") Integer offset){
         return blogService.queryBlogOfFollow(max,offset);
    }

}
